package com.ruoyi.quality.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.sql.QueryHelp;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.quality.domain.InspectionTemplate;
import com.ruoyi.quality.domain.InspectionTemplate;
import com.ruoyi.quality.domain.InspectionTemplateItem;
import com.ruoyi.quality.domain.dto.InspectionTemplateDto;
import com.ruoyi.quality.domain.vo.InspectionTemplateVo;
import com.ruoyi.quality.domain.vo.InspectionTemplateVo;
import com.ruoyi.quality.mapper.InspectionTemplateMapper;
import com.ruoyi.quality.service.InspectionTemplateService;
import com.ruoyi.quality.mapper.InspectionTemplateMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;

/**
 *
 */
@Service
public class InspectionTemplateServiceImpl implements InspectionTemplateService{

    @Resource
    InspectionTemplateMapper inspectionTemplateMapper;


    @Override
    public Page<InspectionTemplateVo> getInspectionTemplateByPage(InspectionTemplateDto pageEntity) {

        MPJLambdaWrapper<InspectionTemplate> wrapper = new MPJLambdaWrapper<InspectionTemplate>()
                .selectAll(InspectionTemplate.class)//查询user表全部字段
                //记的测试阶段
                .eq(InspectionTemplate::getDelFlag, 0)
                .orderByDesc(InspectionTemplate::getCreateTime);
        wrapper = QueryHelp.getPredicateEx(wrapper, InspectionTemplate.class, pageEntity);
        Page<InspectionTemplateVo> userList = inspectionTemplateMapper.selectJoinPage(new Page<>(pageEntity.getCurrentPage(), pageEntity.getPageSize()),   InspectionTemplateVo.class, wrapper);
        return userList;
    }

    @Override
    public void addInspectionTemplate(InspectionTemplateDto testDto) throws ParseException {
        String userName = SecurityUtils.getUsername();
        MPJLambdaWrapper<InspectionTemplate> wrapper = new MPJLambdaWrapper<InspectionTemplate>()
                .eq(InspectionTemplate::getCode, testDto.getCode())
                .ne(InspectionTemplate::getDelFlag, 1);
        InspectionTemplate material = inspectionTemplateMapper.selectOne(wrapper);
        if(null != material) {
            throw new GlobalException("该模板已经存在");
        }
        InspectionTemplate test2 = new InspectionTemplate();
        BeanUtils.copyProperties(testDto, test2, new String[]{"id"});
        test2.setDelFlag(false);
        test2.setCreateBy(userName);
        test2.setCreateTime(new Date());
        inspectionTemplateMapper.insert(test2);
    }

    @Override
    public void updateInspectionTmpFile(InspectionTemplateDto testDto) {
        String userName = SecurityUtils.getUsername();
        MPJLambdaWrapper<InspectionTemplate> wrapper = new MPJLambdaWrapper<InspectionTemplate>()
                .eq(InspectionTemplate::getId, testDto.getId())
                ;
        InspectionTemplate inspectionTemplate = inspectionTemplateMapper.selectOne(wrapper);
        if(null == inspectionTemplate) {
            throw new GlobalException("该InspectionTemplate不存在");
        }
        inspectionTemplate.setFilePath(testDto.getFilePath());
        inspectionTemplate.setFileName(testDto.getFileName());
        inspectionTemplate.setUpdateBy(userName);
        inspectionTemplate.setUpdateTime(new Date());
        inspectionTemplateMapper.updateById(inspectionTemplate);
    }

    @Override
    public void delInspectionTemplate(InspectionTemplateDto testDto) {
        InspectionTemplate material = inspectionTemplateMapper.selectById(testDto.getId());
        if(null == material) {
            throw new GlobalException("该InspectionTemplate不存在");
        }
        material.setDelFlag(true);
        inspectionTemplateMapper.updateById(material);
    }

    @Override
    public void updateInspectionTemplate(InspectionTemplateDto testDto) {
        String userName = SecurityUtils.getUsername();
        MPJLambdaWrapper<InspectionTemplate> wrapper = new MPJLambdaWrapper<InspectionTemplate>();
                ;
        InspectionTemplate inspectionTemplate = inspectionTemplateMapper.selectOne(wrapper);
        if(null == inspectionTemplate) {
            throw new GlobalException("该InspectionTemplate不存在");
        }
        BeanUtils.copyProperties(testDto, inspectionTemplate);
        inspectionTemplate.setUpdateBy(userName);
        inspectionTemplate.setUpdateTime(new Date());
        inspectionTemplateMapper.updateById(inspectionTemplate);
    }
}




